<?php

namespace App\Providers;

use App\Permission;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        Gate::define('doitself', function ($user, $club) {
            if($user->hasRole('root_admin')||$user->hasRole('normalAdmin'))
            {
                return true;
            }
            return $user->owns($club);
        });
        foreach($this->getPermissions() as $permission){
            Gate::define($permission->name,function($user) use ($permission){
                return $user->hasRole($permission->roles);
            });

        }
        Passport::routes();

    }
    protected function getPermissions(){
        return Permission::with('roles')->get();
    }
}
